<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
defined('BASEPATH') || exit('No direct script access allowed');
class Item_lock extends MY_Controller
{
	private $datas;
	private $coins;

	public function __construct()
	{
		parent::__construct();
		$this->verify_user();
		$this->coins = $this->d->rows(array('table' => 'coin', 'where' => 'status=1'));
	}

	public function index()
	{
		$d = $this->result_list(array('table' => 'support', 'where' => 'user_id=' . $this->user['id'] . ' and is_lock=1', 'order' => 'add_time desc', 'url' => 'item_lock/index', 'per_page' => 10));

		foreach ($d['list'] as $k => $v) {
			$d['list'][$k]['coin'] = $this->d->find(array('table' => 'coin', 'where' => 'id=' . $v['coin_id']));
			$d['list'][$k]['item'] = $this->d->find(array('table' => 'items', 'where' => 'id=' . $v['item_id']));

			if (time() < $d['list'][$k]['item']['end']) {
				$d['list'][$k]['stat'] = 0;
			}
			else {
				$d['list'][$k]['stat'] = 1;
			}
		}

		$d['items'] = $this->d->rows(array('table' => 'items', 'where' => 'status=2 and begin>' . time() . ' and is_lock=1'));
		$this->show(array('header', 'user_left', 'item_lock', 'footer'), $d);
	}

	public function balance()
	{
		$item_id = intval($this->input->post('item_id'));

		if (!$item_id) {
			return false;
		}

		$item = $this->d->find(array('table' => 'items', 'where' => 'id=' . $item_id));
		$xnb = $this->d->field(array('field' => 'num', 'table' => 'xnb', 'where' => 'coin_id=' . $item['coin_id'] . ' and user_id=' . $this->user['id']));
		$can = (0 < $item['db_rate'] ? num($xnb / $item['db_rate']) : 0);
		$a = array('xnb' => num($xnb), 'can' => $can);
		echo json_encode($a);
	}

	public function insert()
	{
		$paypwd = $this->input->post('paypwd');
		if (!$paypwd || !$this->pwd->verify($paypwd, $this->user['paypwd'])) {
			ajax_return(1, '安全密码错误');
		}

		$item_id = intval($this->input->post('item_id'));

		if (!$item_id) {
			ajax_return(1, '找不到投资项目');
		}

		$amount = num($this->input->post('amount'));

		if (!$amount) {
			ajax_return(1, '请设置投资数量');
		}

		$this->db->query('set autocommit=0');
		$this->db->query('lock tables t_xnb write, t_support write, t_items write');
		$item = $this->d->find(array('table' => 'items', 'where' => 'id=' . $item_id . ' and status=2 and begin>' . time()));

		if (!$item) {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '找不到投资项目');
		}

		if ($item['is_over'] == 0) {
			if ($item['target_amount'] < ($item['deal'] + $amount)) {
				$this->db->query('rollback');
				$this->db->query('unlock tables');
				ajax_return(1, '该项目还可以投资数量' . num($item['target_amount'] - $item['deal']));
			}
		}

		$coin_id = $item['coin_id'];

		if (!$coin_id) {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '找不到投资币种');
		}

		if (!$item['db_rate']) {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '回报率过低');
		}

		$repay = num($amount / $item['db_rate']);

		if (!$repay) {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '回报数量过低');
		}

		$xnb = $this->d->field(array('field' => 'num', 'table' => 'xnb', 'where' => 'user_id=' . $this->user['id'] . ' and coin_id=' . $coin_id));

		if ($xnb < $amount) {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '余额不足');
		}

		$rs = array();
		$rs[] = $this->db->set('num', 'num-' . $amount, false)->where('user_id', $this->user['id'])->where('coin_id', $coin_id)->update('xnb');
		$rs[] = $this->db->set('support', 'support+1', false)->set('deal', 'deal+' . $amount, false)->where('id', $item_id)->update('items');

		if ($item['is_over'] == 0) {
			$rs[] = $this->db->set('status', 3)->where('id', $item_id)->where('deal>=target_amount')->update('items');
		}

		$data = array('no' => rand(1111, 9999) . time(), 'item_id' => $item_id, 'amount' => $amount, 'repay' => $repay, 'user_id' => $this->user['id'], 'coin_id' => $coin_id, 'is_lock' => 1, 'add_time' => time());
		$rs[] = $this->db->insert('support', $data);

		if (correct($rs)) {
			$this->db->query('commit');
			$this->db->query('unlock tables');
			ajax_return(0, '提交成功', site_url('item_lock'));
		}
		else {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '提交失败');
		}
	}
}

?>
